package com.util;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ItzCompress {

    public void compressDir(File file) throws Exception{
        File[] files = file.listFiles();
        File f = new File(file.getAbsolutePath() + "/temp");
        if(f.exists()){
            DelDir.delAll(f);
        }
        //System.out.println(file.getName());
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory() && !"icons".equals(files[i].getName()) && !"launcher".equals(files[i].getName()) && !"preview".equals(files[i].getName()) && !"wallpaper".equals(files[i].getName())){
                File itzFile = new File(files[i].getAbsolutePath() + ".itz");
                ZipOutputStream zos = null;
                try {
                    zos = new ZipOutputStream(new FileOutputStream(itzFile));
                } catch (Exception e) {
                    e.printStackTrace();
                }


//                compress(files[i],files[i].getName()+"/",zos);
                compress(files[i],"",zos);

                zos.close();
                DelDir.delAll(files[i]);
                itzFile.renameTo(new File(itzFile.getAbsolutePath().substring(0,itzFile.getAbsolutePath().lastIndexOf("."))));
            }
        }
        File itzFile = new File(file.getAbsolutePath() + ".itz");
        ZipOutputStream zos = null;
        try {
            zos = new ZipOutputStream(new FileOutputStream(itzFile));
        } catch (Exception e) {
            e.printStackTrace();
        }

            compress(file,"",zos);
        DelDir.delAll(file);
        zos.close();
        itzFile.renameTo(new File(itzFile.getAbsolutePath().substring(0,itzFile.getAbsolutePath().lastIndexOf(".")-4) + ".itz"));
    }
    public void compress(File f, String baseDir, ZipOutputStream zos){
        if(!f.exists()){
            System.out.println("待压缩的文件目录或文件"+f.getName()+"不存在");
            return;
        }
        File[] fs = f.listFiles();
        BufferedInputStream bis = null;
        //ZipOutputStream zos = null;
        byte[] bufs = new byte[1024*10];
        FileInputStream fis = null;

        try{
            //zos = new ZipOutputStream(new FileOutputStream(zipFile));
            for(int i=0; i<fs.length; i++){
                String fName =  fs[i].getName();
                //System.out.println("压缩：" + baseDir+fName);
                if(fs[i].isFile()){
                    ZipEntry zipEntry = new ZipEntry(baseDir+fName);//
                    zos.putNextEntry(zipEntry);
                    //读取待压缩的文件并写进压缩包里
                    fis = new FileInputStream(fs[i]);
                    bis = new BufferedInputStream(fis, 1024*10);
                    int read = -1;
                    while((read=bis.read(bufs, 0, 1024*10))!=-1){
                        zos.write(bufs, 0, read);
                    }
                    //如果需要删除源文件，则需要执行下面2句
                    fis.close();
                    //fs[i].delete();
                }
                else if(fs[i].isDirectory()){
                    compress(fs[i], baseDir+fName+"/", zos);
                }
            }//end for
        }catch  (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally{
            //关闭流
            try {
                if(null!=bis)
                    bis.close();
                //if(null!=zos)
                //zos.close();
                if(null!=fis)
                    fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

}
